import { Model, type Args, type Mutable } from '@/scripts/client-core'
import { Gender, OrderType, Role } from './enums'

export class RecordModel extends Model {
  readonly createTime: number = 0
  readonly updateTime: number = 0
}

export class NumberIdModel extends Model {
  readonly id: number = 0
}

export class NumberIdRecordModel extends RecordModel {
  readonly id: number = 0
}

export class PageIn extends Model {
  page: number = 1
  size: number = 20
}

export class Page<T> extends Model {
  total: number = 0
  page: number = 0
  content: T[] = []

  override initialize(data: Mutable<this>, args: Args): void {
    if (args[0]) {
      data.content = data.content.map((x: any) => args[0]?.inst(x))
    }
    super.initialize(data, args)
  }

  push(data: T) {
    this.content.push(data)
    this.total += 1
  }

  unshift(data: T) {
    this.content.unshift(data)
    this.total += 1
  }

  filter(predicate: (value: T, index: number, array: T[]) => unknown) {
    this.content = this.content.filter(predicate)
    this.total -= this.total - this.content.length
  }
}

export class DateRange extends Model {
  start: number = 0
  end: number = 0
}

export class LoginIn extends Model {
  username: string = ''
  password: string = ''
}

export type LoginToken = {
  role: Role
}

export type TinyUser = {
  id: number
  name: string
}

export class User extends NumberIdRecordModel {
  username: string = ''
  password: string = ''
  role: Role = Role.USER
  name: string = ''
  avatar: string = ''

  // USER
  amount: string = '0'
  balance: string = '0'

  // COPILOT
  description: string = ''
  gender: Gender = Gender.OTHER
  phone: string = ''
  qrcode: string = ''
  tag: string = ''
}

export class Topup extends NumberIdRecordModel {
  readonly user: number = 0
  description?: string
  amount: string = '0'
  gift: string = '0'
  readonly payment: boolean = false
  readonly amountRecord: string = '0'
  readonly balanceRecord: string = '0'
}

export class Order extends NumberIdRecordModel {
  readonly user: number = 0
  readonly copilot: number = 0
  description?: string
  readonly payment: boolean = false
  type: OrderType = OrderType.NONE
  price: number = 0
  duration: number = 0
  readonly totalPrice: number = 0
  readonly iPrice: number = 0
  readonly cPrice: number = 0
  readonly amountRecord: string = '0'
  readonly balanceRecord: string = '0'
}
